package net.golfklubi.sive;

import java.io.File;
import java.util.List;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.ResourceModel;

public class AllGuessesPanel extends Panel {
    String dirForUsersFiles = new ResourceModel("dirForUsersFiles").getObject();
    private final CompetitionPersisterAndLoader competitionPersisterAndLoader;

    public AllGuessesPanel(final String id, final IModel<List<File>> filesModel, final CompetitionPersisterAndLoader competitionPersisterAndLoader) {
        super(id, filesModel);
        this.competitionPersisterAndLoader = competitionPersisterAndLoader;
        setOutputMarkupPlaceholderTag(true);
        add(addCompetitionsHeader());
        final PropertyListView<File> propertyListView = new PropertyListView<File>("userRow", filesModel.getObject()) {

            @Override
            protected void populateItem(final ListItem<File> item) {
                final String name = item.getModelObject().getName();
                item.add(addUsersGuesses(name));
            }

        };
        add(propertyListView);

    }

    private PropertyListView<Competition> addCompetitionsHeader() {
        final List<Competition> competitions = competitionPersisterAndLoader.loadFromFile("competitions.xml");
        final PropertyListView<Competition> competitionNameListView = new PropertyListView<Competition>("competitionNames", competitions) {

            @Override
            protected void populateItem(final ListItem<Competition> item) {
                item.add(new Label("name", item.getModelObject().name));

            }

        };
        return competitionNameListView;
    }

    private WebMarkupContainer addUsersGuesses(final String filename) {
        final WebMarkupContainer webMarkupContainer = new WebMarkupContainer("rowContainer");
        final LoadableDetachableModel<List<Competition>> competitionsModel = new LoadableDetachableModel<List<Competition>>() {

            @Override
            protected List<Competition> load() {
                return competitionPersisterAndLoader.loadFromFile(dirForUsersFiles + File.separator + filename);
            }

        };
        final PropertyListView<Competition> nicknameAndGuesses = new PropertyListView<Competition>("guesses", competitionsModel) {

            @Override
            protected void populateItem(final ListItem<Competition> item) {
                String guess = String.valueOf(item.getModelObject().getRankingGuess());
                item.add(new Label("guess", guess));

            }

        };
        webMarkupContainer.add(new Label("nickName", competitionsModel.getObject().get(0).nickName));
        webMarkupContainer.add(nicknameAndGuesses);
        return webMarkupContainer;
    }
}
